package com.java.gamePackeg;


import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class Player {
    //玩家姓名
    private String name;
    //现金 开始金额：2000
    private int money = 2000;
    //债务 开始债务：5500
    private int debt = 5500;
    //购买的商品
    private List<PlayerProduct> proList = new ArrayList<PlayerProduct>();
    //商品容量 默认：100个
    private int proCapacity = 100;


    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getDebt() {
        return debt;
    }

    public void setDebt(int debt) {
        this.debt = debt;
    }

    public List<PlayerProduct> getProList() {
        return proList;
    }

    public void setProList(List<PlayerProduct> proList) {
        this.proList = proList;
    }

    public int getProCapacity() {
        return proCapacity;
    }

    public void setProCapacity(int proCapacity) {
        this.proCapacity = proCapacity;
    }

    @Override
    public String toString() {
        return name + '\n' +
                "现金: " + money + "   债务: " + debt + "   出租屋容量" + sumProCount() + "/" + proCapacity + "\n" +
                getProsInfo();
    }

    /**
     * 获取货物的数量
     */
    private int sumProCount() {
        int sum = 0;
        for (PlayerProduct pp : proList) {
            sum += pp.getCount();
        }
        return sum;
    }

    private String getProsInfo() {
        StringBuffer sbuf = new StringBuffer();
        for (int i = 0; i < this.getProList().size(); i++) {
            sbuf.append(i + 1).append("- ").append(proList.get(i)).append("\n");
        }
        return sbuf.toString();
    }

    /**
     * 购买货物
     * buyFlag: true 买的
     * false 送的
     */
    public void buyPro(Product buyPro, int count) {
        buyPro(buyPro, count, true);
    }

    public void buyPro(Product buyPro, int count, boolean buyFlag) {
        //计算需要的钱数
        if (getMoney() < buyPro.getNowPrice() * count && buyFlag) {
            JOptionPane.showMessageDialog(null, "现金不足！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        //判断数量是否超界
        if (getProCapacity() < sumProCount() + count) {
            JOptionPane.showMessageDialog(null, "出租屋的空间不足！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        //判断是否买过
        int ppIndex = getProList().indexOf(buyPro);
        //买过
        if (ppIndex != -1) {
            //价格平均
            int avgPrice = 0;
            if (!buyFlag) {
                //送的
                avgPrice = Math.round((getProList().get(ppIndex).getSumPrice() + 0) * 1.0f
                        / (getProList().get(ppIndex).getCount() + count));
            } else {
                //买的
                avgPrice = Math.round((getProList().get(ppIndex).getSumPrice() + buyPro.getNowPrice() * count) * 1.0f
                        / (getProList().get(ppIndex).getCount() + count));
            }

            getProList().get(ppIndex).setNowPrice(avgPrice);
            getProList().get(ppIndex).setCount(getProList().get(ppIndex).getCount() + count);
        } else {
            //之前没购买
            getProList().add(new PlayerProduct(buyPro, count));
            if (!buyFlag) {
                getProList().get(proList.size() - 1).setNowPrice(0);
            }
        }
        //扣除现金
        if (buyFlag) {
            setMoney(getMoney() - buyPro.getNowPrice() * count);
        }
    }

    /**
     * 卖出
     */
    public void salPro(PlayerProduct salPro, int marketPrice, int count) {
        //检测卖出数量是否有效
        if (salPro.getCount() < count) {
            JOptionPane.showMessageDialog(null, "输入卖出商品数量无效", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        //检测是否买光
        if (salPro.getCount() == count) {
            getProList().remove(salPro);
        } else {
            salPro.setCount(salPro.getCount() - count);
        }
        //加上现金
        setMoney(getMoney() + marketPrice * count);
    }
}
